package admin.controllers;

import helpers.CookieHelper;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import admin.models.TemplateModel;
import entities.Template;

public class TemplateDeleteServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		response.setContentType("text/html");
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		
		try {
			TemplateModel templateModel = TemplateModel.getInstance();
			Template template = templateModel.get(id);
			templateModel.delete(template);
			CookieHelper.setCookie(response, "success", "Delete success template " + id);
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		response.sendRedirect("/admin/template/list");
	}
}
